#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// PandoraFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using UnityEngine;
using UnityEditor;
using System;
using Pandora;

namespace PandoraEditor.Window
{
    public class FindComponent : IDrawPanel
    {
        public FindComponent()
        {

        }

        private Vector2 m_v2ScrollView;
        private Vector2 m_v2ScrollViewRight;
        private static string s_strComponentName = "";
        private static string s_strResult = "";
        private static System.Type s_type;

        public override void DrawPanel()
        {
            GUILayout.BeginHorizontal();
            {
                GUILayout.Space(10);
                GUILayout.BeginVertical(GUILayout.MaxWidth(300));
                {
                    EditorGUILayout.LabelField("");
                    EditorGUILayout.LabelField("查找包含指定组件的对象", EditorStyles.boldLabel);

                    GUILayout.BeginVertical("box", GUILayout.Height(m_Window.position.height - 150));
                    {

                        m_v2ScrollView = EditorGUILayout.BeginScrollView(m_v2ScrollView);
                        {

                            s_strComponentName = EditorGUILayout.TextField("输入组件名：", s_strComponentName);

                            EditorGUILayout.LabelField("");

                            if (GUILayout.Button("在当前场景查找"))
                            {
                                DoFindCompInCurrentScene();
                            }

                            if (GUILayout.Button("Find In All Assets"))
                            {
                                DoFindCompInAllAssets();
                            }

                            if (GUILayout.Button("清空"))
                            {
                                s_strResult = "";
                            }

                        }
                        EditorGUILayout.EndScrollView();

                    }
                    EditorGUILayout.EndVertical(); //左侧内部布局


                }
                GUILayout.EndVertical(); //左侧布局


                GUILayout.BeginVertical(GUILayout.MaxWidth(800));
                {
                    EditorGUILayout.LabelField("");
                    EditorGUILayout.LabelField("查找结果：", EditorStyles.boldLabel);

                    GUILayout.BeginVertical("box", GUILayout.Height(m_Window.position.height - 150));
                    {

                        m_v2ScrollViewRight = EditorGUILayout.BeginScrollView(m_v2ScrollViewRight);
                        {

                            GUILayout.TextArea(s_strResult);
                        }
                        EditorGUILayout.EndScrollView();

                    }
                    EditorGUILayout.EndVertical(); //右侧内部布局


                }
                GUILayout.EndVertical(); //左侧布局
            }
            GUILayout.EndHorizontal();      //最外层布局
        }

        void DoFindCompInCurrentScene()
        {
            if (!CheckType())
                return;

            s_strResult = "当前场景查找结果：\n";
            PdrWindowHelper.FindGameObjectInCurrentScene((go) =>
               {
                   //Debug.LogFormat ("go:{0}", go.name);
                   string strLog = FindCompInGameObj(go);
                   if (!string.IsNullOrEmpty(strLog))
                   {
                       s_strResult += strLog;
                       Debug.LogError(strLog);
                   }
               });
        }

        void DoFindCompInAllAssets()
        {
            if (!CheckType())
                return;

            s_strResult = "当前所有资源中查找结果：\n";
            PdrWindowHelper.CheckAllPrefabs("当前所有资源中查找结果", (go) =>
                {
                    //Debug.LogFormat ("go:{0}", go.name);
                    //						string strLog = FindCompInGameObj(go);
                    //						if(!string.IsNullOrEmpty (strLog))
                    //						{
                    //							Debug.LogError (strLog);
                    //						}
                });
        }

        bool CheckType()
        {
            s_type = Type.GetType(s_strComponentName);
            if (s_type == null)
            {
                s_strResult = "请输入正确的类型！";
                return false;
            }
            return true;
        }


        string FindCompInGameObj(GameObject go)
        {
            string strLog = "";
            foreach (Component component in go.GetComponents(typeof(Component)))
            {
                if (component == null)
                {
                    strLog += "Missing Component in : " + PdrUtil.GetFullPathOfGO(go) + "\n";
                }
                else if (component.GetType() == s_type)
                {
                    strLog += "FindComponent Component in : " + PdrUtil.GetFullPathOfGO(go) + "\n";
                }
            }
            for (int i = 0; i < go.transform.childCount; i++)
            {
                Transform child = go.transform.GetChild(i);
                if (child != null)
                {
                    strLog += FindCompInGameObj(child.gameObject);
                }
            }
            return strLog;
        }

    }
}

